package com.richest.api.controller.cn.merchant.good;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.richest.api.service.IAdmartService;
import com.richest.api.service.IGoodService;
import com.richest.bean.AdmartBean;
import com.richest.bean.GoodBean;
import com.richest.util.CallResults;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 商户菜品管理接口
 * 
 * @author zhaohongchao 2019年3月29日 下午2:10:52
 * @email 70375739@qq.com
 */
@RestController
@RequestMapping("GoodManageController")
@Api(value = "商户菜品管理接口")
@EnableAutoConfiguration
public class ApiGoodManageController {
	@Autowired
	private IGoodService goodService;
	@Autowired
	private IAdmartService admartService;

	/**
	 * 商户端订单列表
	 * 
	 * @author zhaohongchao 2019年4月3日 上午8:49:30
	 * @email 70375739@qq.com
	 * @param status
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "getGoodList", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "菜品管理列表", value = "菜品管理列表", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "admart_id", value = "商户id", required = true, paramType = "query", dataType = "Long"),
			@ApiImplicitParam(name = "shelve", value = "是否上架：1=上架/0=下架", required = true, paramType = "query", dataType = "Integer") })
	public CallResults getGoodList(@RequestParam Long admart_id, @RequestParam Integer shelve) {
		AdmartBean admartBean = admartService.selectById(admart_id);
		if (null == admartBean) {
			return CallResults.error("不存在有效的店铺信息");
		}
		return CallResults.ok().put("data", goodService.getGoodListByShelve(admart_id, shelve));
	}

	/**
	 * 菜品上架/下架
	 * 
	 * @author zhaohongchao 2019年4月3日 上午8:49:30
	 * @email 70375739@qq.com
	 * @param status
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "upperOrLowerShelf", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "菜品上架/下架", value = "菜品管理列表", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "good_id", value = "商户id", required = true, paramType = "query", dataType = "Long"),
			@ApiImplicitParam(name = "shelve", value = "true=上架/false=下架", required = true, paramType = "query", dataType = "Boolean"),
			@ApiImplicitParam(name = "user_id", value = "用户id", required = true, paramType = "query", dataType = "String") })
	public CallResults upperOrLowerShelf(@RequestParam Long good_id, @RequestParam Boolean shelve,
			@RequestParam String user_id) {
		GoodBean goodBean = goodService.selectById(good_id);
		if (null == goodBean) {
			return CallResults.error("不存在有效的菜品信息");
		}
		goodBean.setShow_in_shelve(shelve);
		goodBean.setUpdate_by(user_id);
		goodBean.setUpdate_time(new Date());
		goodService.upperOrLowerShelf(goodBean);
		return CallResults.ok();
	}

	/**
	 * 删除菜品
	 * 
	 * @author zhaohongchao 2019年4月4日 下午3:00:09
	 * @email 70375739@qq.com
	 * @param good_id
	 * @param shelve
	 * @param user_id
	 * @return
	 */
	@RequestMapping(value = "deleteGood", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "删除菜品", value = "菜品管理列表", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "good_id", value = "商户id", required = true, paramType = "query", dataType = "Long"),
			@ApiImplicitParam(name = "user_id", value = "用户id", required = true, paramType = "query", dataType = "String") })
	public CallResults deleteGood(@RequestParam Long good_id, @RequestParam Boolean shelve,
			@RequestParam String user_id) {
		GoodBean goodBean = goodService.selectById(good_id);
		if (null == goodBean) {
			return CallResults.error("不存在有效的菜品信息");
		}
		goodBean.setActivate(false);
		goodBean.setUpdate_by(user_id);
		goodBean.setUpdate_time(new Date());
		goodService.deleteGood(goodBean);
		return CallResults.ok();
	}

	/**
	 * 添加菜品
	 * 
	 * @author zhaohongchao 2019年4月4日 下午3:00:09
	 * @email 70375739@qq.com
	 * @param good_id
	 * @param shelve
	 * @param user_id
	 * @return
	 */
	@RequestMapping(value = "addGood", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "添加菜品", value = "添加菜品", httpMethod = "POST")
	@ApiImplicitParam(name = "goodBean", value = "商户id", required = true, paramType = "query", dataType = "GoodBean")
	public CallResults addGood(@RequestBody GoodBean goodBean) {
		goodService.addGood(goodBean);
		return CallResults.ok();
	}
}
